<?php

namespace app\api\controller;

use app\common\validate\IDMustBePostiveInt;
use app\common\DictConstant;
use app\api\service\weixin\OrderPay as PayService;

class Pay extends Base
{
    // 订单支付
    public function getPreOrder($id = '')
    {
        (new IDMustBePostiveInt())->goCheck();

        $pay_method = input('pay_method');
        if (empty($pay_method)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'支付方式(pay_method) 不能为空');
        }
        $device = input('device');
        if (empty($device)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'设备类型(device) 不能为空');
        }

        if ($pay_method == 1) {
            if ($device == 'WeiXin') {
                // 微信支付
                $pay = new \app\api\service\weixin\OrderPay($id);
            } else {
                // 微信APP支付
                $pay = new \app\api\service\weixinApp\OrderPay($id);
            }

            return $pay->pay();
        } elseif ($pay_method == 2) {
            // 支付宝支付
            $pay = new \app\api\service\alipay\OrderPay($id);
            return $pay->pay($id);
        }
    }

    // 订单余额支付
    public function balancePay($id = ''){
        (new IDMustBePostiveInt())->goCheck();
        $formId = '';
        if(input('post.form_id')){
            $formId = input('post.form_id');
        }
        $pay = new PayService($id);
        $payresult = $pay->balancepay($formId);
        if($payresult['Balance'] == DictConstant::BALANCE_TRUE && $payresult['isPay']){
            return show(config('status.SUCCESS_STATUS'),'支付成功',$payresult);
        }elseif ($payresult['Balance'] == DictConstant::BALANCE_FALSE){
            return show(config('status.ERROR_STATUS'),'账户余额不足',$payresult);
        }else{
            return show(config('status.ERROR_STATUS'),'订单异常',$payresult);
        }
    }

    // 余额充值
    public function rechargePay()
    {
        $param = self::getHttpParam();
        if (empty($param->type)){
            return show(config('status.ERROR_STATUS'),self::NOT_PARAM,'支付方式(type) 不能为空');
        }

        if ($param->type == 1) {
            if ($param->device == 'WeiXin') {
                // 微信支付
                $pay = new \app\api\service\weixin\RechargePay();
            } else {
                // 微信APP支付
                $pay = new \app\api\service\weixinApp\RechargePay();
            }

            return $pay->pay($param);
        } elseif ($param->type == 2) {
            // 支付宝支付
            $pay = new \app\api\service\alipay\RechargePay();

            return $pay->pay($param);
        }
    }

    // 微信 - 订单支付结果回调地址
    public function wxOrderNotify(){
        //通知频率为15/15/30/180/1800/1800/1800/1800/3600，单位：秒
        //1.检测库存量
        //2.更新订单状态
        //3.减库存
        //如果成功处理，则向微信返回成功处理的消息

        //特点：post；xml格式；不会携带参数

        $notify = new \app\api\service\weixin\OrderNotify();
        $notify->Handle();
    }

    // 支付宝 - 订单支付结果回调地址
    public function alipayOrderNotify()
    {
        $notify = new \app\api\service\alipay\OrderNotify();
        $notify->notify();
    }

    // 微信 - 充值结果回调地址
    public function wxRechargeNotify()
    {
        $notify = new \app\api\service\weixin\RechargeNotify();
        $notify->Handle();
    }

    // 支付宝 - 充值结果回调地址
    public function alipayRechargeNotify()
    {
        $notify = new \app\api\service\alipay\RechargeNotify();
        $notify->notify();
    }

    // 微信 - 升级会员等级支付回调地址
    public function wxMemberlevelNotify()
    {
        $notify = new \app\api\service\weixin\MemberlevelNotify();
        $notify->Handle();
    }

    // 支付宝 - 升级会员等级支付回调地址
    public function alipayMemberlevelNotify()
    {
        $notify = new \app\api\service\alipay\MemberlevelNotify();
        $notify->notify();
    }
}